@extends('backend.layouts.base')
@section('title', '幻灯片管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">幻灯片管理</a> > 编辑幻灯片组
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-main">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">编辑幻灯片组</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="描述" prop="desc">
                            <el-input v-model="form.desc" :rows="5" type="textarea" ref="desc"></el-input>
                        </el-form-item>
                    </div>
                </div>

            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
        </div>
    </div>
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:0,
                    form:{
                        name:'',
                        desc:''
                    },
                    rules:{
                        name:{ required: true, message: '请输入名称'}
                    },
                    submit_url:''

                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('sys.carousel.update') !!}`;
                    this.getData();
                }
                else {
                    this.submit_url = `{!! absolute_admin_url('sys.carousel.create') !!}`;
                }
            },
            methods: {
                goParent() {
                    window.location.href = `{!! absolute_admin_url('sys.carousel.index') !!}`;
                },
                getData() {
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('sys.carousel.getDetail') !!}',{id:this.id}).then(function (response) {
                            if (response.data.result){
                                this.form = {
                                    ...response.data.data,
                                };
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                submitForm(formName) {
                    let that = this;
                    let json = {
                        name:this.form.name,
                        desc:this.form.desc
                    };
                    if(this.id) {
                        json.id = this.id
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{params:json}).then(response => {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goParent();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
            },
        })

    </script>
@endsection
